package com.heima.wemedia.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService{
    /**
     * 删除敏感词
     *
     * @param id
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验
        if(id == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.业务处理
        WmSensitive sensitive = getById(id);
        if(sensitive == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        removeById(id);

        //3.封装结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询敏感词列表
     *
     * @param dto
     */
    @Override
    public ResponseResult lists(ChannelDto dto) {
        //1.参数校验
        if(dto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.构建分页构造器
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(dto.getName()))
            lqw.like(WmSensitive::getSensitives,dto.getName());

        //3.查询数据
        page(page,lqw);

        //4.封装结果返回
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 保存敏感词
     *
     * @param dto
     */
    @Override
    public ResponseResult saves(WmSensitive dto) {
        //1.参数校验
        if(dto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.业务处理
        //2.1已存在的敏感词则不能保存
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(sensitive != null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        //2.2保存
        dto.setCreatedTime(new Date());
        save(dto);

        //3.结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param dto
     */
    @Override
    public ResponseResult updates(WmSensitive dto) {
        //1.参数校验
        if(dto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.业务处理
        updateById(dto);

        //3.封装结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
